﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BugNET.Entities;

namespace BugNET.BLL.Comparers
{
    public class WikiPageComparer : IComparer<Wiki>
    {
        /// <summary>
        /// Sorting column
        /// </summary>
        private readonly string _sortColumn;
        /// <summary>
        /// Reverse sorting
        /// </summary>
        private readonly bool _reverse;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectComparer"/> class.
        /// </summary>
        /// <param name="sortEx">The sort ex.</param>
        /// <param name="ascending">The ascending.</param>
        public WikiPageComparer(string sortEx, bool ascending)
        {
            if (!String.IsNullOrEmpty(sortEx))
            {
                _reverse = ascending;
                _sortColumn = sortEx;
            }
        }

        /// <summary>
        /// Equalses the specified x.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns></returns>
        public bool Equals(Project x, Project y)
        {
            return x.Id == y.Id;
        }

        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>
        /// Value Condition Less than zero<paramref name="x"/> is less than <paramref name="y"/>.Zero<paramref name="x"/> equals <paramref name="y"/>.Greater than zero<paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        public int Compare(Wiki x, Wiki y)
        {
            var retVal = 0;
            switch (_sortColumn)
            {
                case "Name":
                    retVal = String.Compare(x.WikiPageName, y.WikiPageName, StringComparison.InvariantCultureIgnoreCase);
                    break;
                case "Creator":
                    retVal = String.Compare(x.UserCreated, y.UserCreated, StringComparison.InvariantCultureIgnoreCase);
                    break;
                case "CreatedDate":
                    retVal = DateTime.Compare(x.DateCreated, y.DateCreated);
                    break;
                case "SortOrder":
                    retVal = x.SortOrder - y.SortOrder;
                    break;

            }
            return (retVal * (_reverse ? -1 : 1));
        }
    }
}
